package cc.rengu.igas.bsps.common.dao.impl;

import cc.rengu.igas.bsps.common.dao.MchntAcctSumMapper;
import cc.rengu.igas.bsps.common.entity.BspsMchntAcctSum;
import cc.rengu.igas.bsps.common.entity.MchntAcctSum;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.utility.bean.PageInfo;
import cc.rengu.oltp.utility.bean.PageResult;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 商户入账结果表操作接口实现类
 * Created by 王成 on 2018/10/8.
 */
public class MchntAcctSumMapperImpl implements MchntAcctSumMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());
    private String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppConfigConstant.DATABASE_CONF_NODE, AppConfigConstant.CSMC_POOL_NAME);

    @Override
    public List<MchntAcctSum> selectMchntAcctSumBySettleDateAndMchntNo(String mchntNo, String settleStartDate, String settleEndDate) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.CSMC_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntAcctSumBySettleDateAndMchntNo");
        MchntAcctSum mchntAcctSum = new MchntAcctSum();
        mchntAcctSum.setMchntNo(mchntNo);
        mchntAcctSum.setBatchDate(settleStartDate);
        mchntAcctSum.setRemark1(settleEndDate);
        List<Object> objectList = dbsUtil.dbsFetchLimitListData(sqlStr, mchntAcctSum, MchntAcctSum.class);
        if (null == objectList || objectList.isEmpty()) {
            rglog.error("根据商户号<{}>查询清算日期<{}>至<{}>的商户入账结果信息失败!", mchntNo, settleStartDate, settleEndDate);
            return null;
        }
        return objectList.stream().map(item -> (MchntAcctSum) (item)).collect(Collectors.toList());
    }

    @Override
    public PageResult selectMchntSettlementFailQuery(String mchntNo, String settleStartDate, String settleEndDate, PageInfo pageInfo) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.CSMC_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntSettlementFailQuery");
        BspsMchntAcctSum mchntAcctSum = new BspsMchntAcctSum();
        mchntAcctSum.setMchntNo(mchntNo);
        mchntAcctSum.setBatchDate(settleStartDate);
        mchntAcctSum.setRemark1(settleEndDate);

        return dbsUtil.dbsFetchPageListData(sqlStr, mchntAcctSum, BspsMchntAcctSum.class, pageInfo);
    }

    @Override
    public BspsMchntAcctSum selectMchntSettlementFailSum(String mchntNo, String settleStartDate, String settleEndDate) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.CSMC_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "selectMchntSettlementFailSum");
        BspsMchntAcctSum mchntAcctSum = new BspsMchntAcctSum();
        mchntAcctSum.setMchntNo(mchntNo);
        mchntAcctSum.setBatchDate(settleStartDate);
        mchntAcctSum.setRemark1(settleEndDate);
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, mchntAcctSum, mchntAcctSum);
        if (0 != iReturnCode) {
            rglog.error("获取用户信息失败,iReturnCode:<{}>", iReturnCode);
            return null;
        }
        return mchntAcctSum;
    }
}
